/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x.test;

/**
 *
 * @author eros
 */







import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.io.Serializable;

/**
 * <p>The <code>FilesRecordsManager</code> class is responsible for constructing
 * the list of <code>FilesGroupRecordBean</code> beans which will be bound to a
 * ice:dataTable JSF component.  This construction process is currently static
 * but could easily be configured to work with Hibernate or DAO implementation.
 * </p>
 * <p/>
 * <p>Large data sets could be handled by adding an ice:dataPaginator.
 * Alternatively the dataTable could also be hidden and the dataTable could be
 * added to scrollable ice:panelGroup. </p>
 * <p/>
 * <p>All default style allocation can be changed in this class.  The
 * application /lib/sytle.css file can be edited to change the style properties
 * of the table.</p>
 */
public class FilesRecordsManager implements Serializable {

    private ArrayList inventoryGroupItemBeans;

    private boolean isInit;

    // css style related constants
    public static final String GROUP_INDENT_STYLE_CLASS = "groupRowIndentStyle";
    public static final String GROUP_ROW_STYLE_CLASS = "groupRowStyle";
    public static final String CHILD_INDENT_STYLE_CLASS = "childRowIndentStyle";
    public static final String CHILD_ROW_STYLE_CLASS = "childRowStyle";
    // toggle for expand contract
    public static final String CONTRACT_IMAGE = "tree_nav_top_close_no_siblings.gif";
    public static final String EXPAND_IMAGE = "tree_nav_top_open_no_siblings.gif";

    public FilesRecordsManager() {
        init();
    }

    private void init() {

        // check if manager has been initiated
        if (isInit) {
            return;
        }
        isInit = true;

        // initiate the list
        if (inventoryGroupItemBeans != null) {
            inventoryGroupItemBeans.clear();
        } else {
            inventoryGroupItemBeans = new ArrayList(10);
        }

        Application application =
                FacesContext.getCurrentInstance().getApplication();

       StyleBean styleBean =  ((StyleBean) application.createValueBinding("#{styleBean}").getValue(FacesContext.getCurrentInstance()));

        /**
         * Build the array list group items.  Currently static but could be easily
         * bound to a database.
         */

        // Project Budget's group



        FilesGroupRecordBean filesRecordGroup =
                new FilesGroupRecordBean(GROUP_INDENT_STYLE_CLASS,GROUP_ROW_STYLE_CLASS, styleBean,  EXPAND_IMAGE, CONTRACT_IMAGE,
                        inventoryGroupItemBeans, true);
        filesRecordGroup.setDescription("bean.expandTable.parentGroup1.description");
        filesRecordGroup.setModified("bean.expandTable.parentGroup1.modified");
        filesRecordGroup.setCreated("bean.expandTable.parentGroup1.created");
        filesRecordGroup.setSize("bean.expandTable.parentGroup1.size");
        filesRecordGroup.setKind("bean.expandTable.parentGroup1.kind");
        filesRecordGroup.setVersion("bean.expandTable.parentGroup1.version");
        // add Project budget's children
        FilesGroupRecordBean childFilesGroup =
                new FilesGroupRecordBean(CHILD_INDENT_STYLE_CLASS,
                        CHILD_ROW_STYLE_CLASS);
        childFilesGroup.setDescription("bean.expandTable.parentGroup1.childGroup1.description");
        childFilesGroup.setModified("bean.expandTable.parentGroup1.childGroup1.modified");
        childFilesGroup.setCreated("bean.expandTable.parentGroup1.childGroup1.created");
        childFilesGroup.setSize("bean.expandTable.parentGroup1.childGroup1.size");
        childFilesGroup.setKind("bean.expandTable.parentGroup1.childGroup1.kind");
        childFilesGroup.setVersion("bean.expandTable.parentGroup1.childGroup1.version");
        filesRecordGroup.addChildFilesGroupRecord(childFilesGroup);
        childFilesGroup =
                new FilesGroupRecordBean(CHILD_INDENT_STYLE_CLASS,
                        CHILD_ROW_STYLE_CLASS);
        childFilesGroup.setDescription("bean.expandTable.parentGroup1.childGroup2.description");
        childFilesGroup.setModified("bean.expandTable.parentGroup1.childGroup2.modified");
        childFilesGroup.setCreated("bean.expandTable.parentGroup1.childGroup2.created");
        childFilesGroup.setSize("bean.expandTable.parentGroup1.childGroup2.size");
        childFilesGroup.setKind("bean.expandTable.parentGroup1.childGroup2.kind");
        childFilesGroup.setVersion("bean.expandTable.parentGroup1.childGroup2.version");
        filesRecordGroup.addChildFilesGroupRecord(childFilesGroup);

        // Project Development's group
       
        childFilesGroup =
                new FilesGroupRecordBean(CHILD_INDENT_STYLE_CLASS,
                        CHILD_ROW_STYLE_CLASS);
        childFilesGroup.setDescription("bean.expandTable.parentGroup2.childGroup1.description");
        childFilesGroup.setModified("bean.expandTable.parentGroup2.childGroup1.modified");
        childFilesGroup.setCreated("bean.expandTable.parentGroup2.childGroup1.created");
        childFilesGroup.setSize("bean.expandTable.parentGroup2.childGroup1.size");
        childFilesGroup.setKind("bean.expandTable.parentGroup2.childGroup1.kind");
        childFilesGroup.setVersion("bean.expandTable.parentGroup2.childGroup1.version");
        filesRecordGroup.addChildFilesGroupRecord(childFilesGroup);
        childFilesGroup =
                new FilesGroupRecordBean(CHILD_INDENT_STYLE_CLASS,
                        CHILD_ROW_STYLE_CLASS);
        childFilesGroup.setDescription("bean.expandTable.parentGroup2.childGroup2.description");
        childFilesGroup.setModified("bean.expandTable.parentGroup2.childGroup2.modified");
        childFilesGroup.setCreated("bean.expandTable.parentGroup2.childGroup2.created");
        childFilesGroup.setSize("bean.expandTable.parentGroup2.childGroup2.size");
        childFilesGroup.setKind("bean.expandTable.parentGroup2.childGroup2.kind");
        childFilesGroup.setVersion("bean.expandTable.parentGroup2.childGroup2.version");
        filesRecordGroup.addChildFilesGroupRecord(childFilesGroup);
        childFilesGroup =
                new FilesGroupRecordBean(CHILD_INDENT_STYLE_CLASS,
                        CHILD_ROW_STYLE_CLASS);
        childFilesGroup.setDescription("bean.expandTable.parentGroup2.childGroup3.description");
        childFilesGroup.setModified("bean.expandTable.parentGroup2.childGroup3.modified");
        childFilesGroup.setCreated("bean.expandTable.parentGroup2.childGroup3.created");
        childFilesGroup.setSize("bean.expandTable.parentGroup2.childGroup3.size");
        childFilesGroup.setKind("bean.expandTable.parentGroup2.childGroup3.kind");
        childFilesGroup.setVersion("bean.expandTable.parentGroup2.childGroup3.version");
        filesRecordGroup.addChildFilesGroupRecord(childFilesGroup);

       
        childFilesGroup =
                new FilesGroupRecordBean(CHILD_INDENT_STYLE_CLASS,
                        CHILD_ROW_STYLE_CLASS);
        childFilesGroup.setDescription("bean.expandTable.parentGroup3.childGroup1.description");
        childFilesGroup.setModified("bean.expandTable.parentGroup3.childGroup1.modified");
        childFilesGroup.setCreated("bean.expandTable.parentGroup3.childGroup1.created");
        childFilesGroup.setSize("bean.expandTable.parentGroup3.childGroup1.size");
        childFilesGroup.setKind("bean.expandTable.parentGroup3.childGroup1.kind");
        childFilesGroup.setVersion("bean.expandTable.parentGroup3.childGroup1.version");
        filesRecordGroup.addChildFilesGroupRecord(childFilesGroup);

    }

    /**
     * Cleans up the resources used by this class.  This method could be called
     * when a session destroyed event is called.
     */
    public void dispose() {
        isInit = false;
        // clean up the array list
        if (inventoryGroupItemBeans != null) {
            FilesGroupRecordBean tmp;
            ArrayList tmpList;
            for (int i = 0; i < inventoryGroupItemBeans.size(); i++) {
                tmp = (FilesGroupRecordBean) inventoryGroupItemBeans.get(i);
                tmpList = tmp.getChildFilesRecords();
                if (tmpList != null) {
                    tmpList.clear();
                }
            }
            inventoryGroupItemBeans.clear();
        }
    }

    /**
     * Gets the list of FilesGroupRecordBean which will be used by the
     * ice:dataTable component.
     *
     * @return array list of parent FilesGroupRecordBeans
     */
    public ArrayList getFilesGroupRecordBeans() {
        return inventoryGroupItemBeans;
    }
}